﻿using System.IO.Ports;
using SimpleTool.Base;
using static SimpleTool.SerialTool.SerialControl.SerialViewModel;

namespace SimpleTool.SerialTool.SerialControl
{

    public class CommandSectionModel
    {
        public delegate void CommandChangeReceive(object? sender, CommandChangeEventHandle e);

        /// <summary>
        /// defualt duration 0; default readonly true
        /// </summary>
        public sealed class CommandStruct
        {
            public string Command { set; get; }
            public string Alias { set; get; }
            public int Duration { set; get; }
            public int Order { set; get; }

            public CommandStruct(string Cmd = "", int Dura = 0, string Alia = "", int Ord = 0)
            {
                Command = Cmd; Duration = Dura; Alias = Alia; Order = Ord;
            }
        }

        public sealed class AlterCommandStruct
        {
            public CommandStruct AlterCommand { set; get; }

            public int AlterIndex { set; get; }

            public AlterCommandStruct(CommandStruct newStruct, int newIndex)
            {
                AlterCommand = newStruct; AlterIndex = newIndex;

            }
        }

        public List<CommandStruct> CommandSelectionsCache = []; //  缓存选中的指令项
    }

    public class ConfigSectionModel
    {

        //  记录所选选项，与下拉框期望展示的默认选项区分开来，避免数据污染
        public sealed class ConfigSelectedRecord
        {
            private ConfigSelectedRecord() { }

            public static readonly ConfigSelectedRecord Com = new();

            public string PortName = string.Empty;

            public int BaudRate = 115200;

            public StopBits StopBit = StopBits.One;

            public Parity ParityBit = Parity.None;

            public int DataBit = 8;

            public Handshake Handshake = Handshake.None;
        }

        //  对应关系字典，对应每个属性的字符串名称和实际的串口所需的类型数据
        public sealed class SerialConfigInfoMap
        {

            public static readonly SerialConfigInfoMap Com = new();

            public Dictionary<string, string> SerialComNameMap = [];

            public Dictionary<string, int> SerialComBaudMap = [];

            public Dictionary<string, int> SerialComDataMap = [];

            public Dictionary<string, Parity> SerialComParityMap = [];

            public Dictionary<string, StopBits> SerialComStopMap = [];

            public Dictionary<string, Handshake> SerialComHandShakeMap = [];
        }

        public readonly Dictionary<string, string> TableReserveDuty = new()
        {
            {"serial_config", "CREATE TABLE IF NOT EXISTS serial_config(" +
                "port_name TEXT PRIMARY KEY," +
                "baud_rate TEXT NOT NULL COLLATE NOCASE," +
                "data_bits TEXT NOT NULL COLLATE NOCASE," +
                "stop_bits TEXT NOT NULL COLLATE NOCASE," +
                "parity_bits TEXT NOT NULL COLLATE NOCASE," +
                "hand_shake TEXT NOT NULL COLLATE NOCASE)"}
            ,

            {"serial_command", "CREATE TABLE IF NOT EXISTS serial_command(" +
             "command_name TEXT PRIMARY KEY," +
             "command_alias TEXT NOT NULL COLLATE NOCASE," +
             "command_interval TEXT NOT NULL COLLATE NOCASE)"}
        };
    }

    public sealed class HigtLightOptions : Notify
    {
        private string _HL_Name = string.Empty;

        private string _HL_Info = string.Empty;

        public string HL_Name
        {
            set
            {
                _HL_Name = value;
                propertyChangeSend(nameof(HL_Name));
            }
            get
            {
                return _HL_Name;
            }
        }

        public string HL_Info
        {
            set
            {
                _HL_Info = value;
                propertyChangeSend(nameof(HL_Info));
            }
            get
            {
                return _HL_Info;
            }
        }
    }
}
